const LOADING_IMAGE = (Site.DEBUG) ? 'http://localhost:8081/live3dstatic/images/loading.gif' : 'http://www.projectlive3d.com/live3dstatic/images/loading.gif'
const PTZ_FILL_STYLES = new Array( "red", "blue", "yellow", "green", "orange", "purple");
const PTZ_STROKE_STYLES = new Array("black", "black","black","black","black","black");
const PTZ_LINE_WIDTH = 2;

const PTZ_3D_ICONS = new Array( "red","blue","yellow","green","orange", "purple");


function PTZBuild2D(baseURL){
	this.baseURL = baseURL;
	this.points = new Array();
	this.ptzValuesReady = false;
}

PTZBuild2D.prototype.initPoints = function(image){
	if (typeof(image) == 'undefined'){
		var image = img;
	}
	
	this.width = image.width;
	this.height = image.height;
	
	var type = "";
	for (i=0; i< document.corrForm.correspondence.length; i++)
	{
		if (document.corrForm.correspondence[i].checked==true)
		{
			type = document.corrForm.correspondence[i].value;
			break;
		}
	}
	if (type != ""){
		this.numPoints = parseInt(type);
		this.points = new Array();
		if (this.numPoints == 1){
			this.points[0] = new Point2D(image.width / 2, image.height / 2, true);
			this.points[0].setStyle(PTZ_FILL_STYLES[0], PTZ_STROKE_STYLES[0], PTZ_STROKE_STYLES[0], PTZ_LINE_WIDTH);
			this.points[0].movedPoint = false;
		} else {
			var theta = 0;
			var thetaStep = 2 * Math.PI / this.numPoints;
			for (var i = 0; i < this.numPoints; i++){
				var x = image.width / 2 + Math.cos(theta) * image.width / 4;
				var y = image.height / 2 + Math.sin(theta) * image.height / 4;
				this.points[i] = new Point2D(x, y, true);
				this.points[i].setStyle(PTZ_FILL_STYLES[i], PTZ_STROKE_STYLES[i], PTZ_STROKE_STYLES[i], PTZ_LINE_WIDTH);
				this.points[i].movedPoint = false;
				theta = theta + thetaStep;
			}
		}
		
	}
	
	this.drawEverything();
}

PTZBuild2D.prototype.initCanvas = function(image){
	this.initPoints(image);
	
	var canvas = document.getElementById('canvasGroup');
	this.canvasOffset = findPos(canvas);
	
	var me = this;
	canvas.addEventListener("mousedown", function(ev){me.mousedown(ev)}, false);
	canvas.addEventListener("mouseup",   function(ev){me.mouseup(ev)}, false);
	canvas.addEventListener("mousemove", function(ev){me.mousemove(ev)}, false);	
	
	this.drawEverything();
}

PTZBuild2D.prototype.takeScreenshot = function(){
	// get the current PTZ values.
	this.dumpCurrPTZValues();
	
	var url = this.baseURL + '/-wvhttp-01-/GetOneShot?image_size?=640x480';
	url = url + Math.floor(Math.random() * 30000);
	
	// change the canvas image.
	var me = this;
	loadImage(url, function(){
				me.initCanvas(img);		
			  }, null);
	
	document.getElementById('PTZappletGroup').style.display = 'none';
	document.getElementById('canvasGroup').style.display = 'block';
	
}

PTZBuild2D.prototype.backToPTZApplet = function(){
	document.getElementById('PTZappletGroup').style.display = 'block';
	document.getElementById('canvasGroup').style.display = 'none';
	this.ptzValuesReady = false;
	
	loadImage(LOADING_IMAGE, function(){}, null);
}

PTZBuild2D.prototype.dumpCurrPTZValues = function(){
	var http = createRequest();
	var url = "getCurrPtz?baseURL=" + this.baseURL;
	http.open("GET", url, true);
	var me = this;
	http.onreadystatechange = function(){
		if(http.readyState == 4){
			var ptzString = http.responseText;
			var ptz = ptzString.split(" ");
			document.getElementById('pan').value = ptz[0];
			document.getElementById('tilt').value = ptz[1];
			document.getElementById('zoom').value = ptz[2];
			me.ptzValuesReady = true;
		}
	};
	http.send(null);
}



// Tracks mouse movement and updates point locations.
PTZBuild2D.prototype.mouseup = function(ev) {
	if (this.draggedPoint){	
		this.draggedPoint.setDragged(false);
		this.draggedPoint = null;
	}
}

PTZBuild2D.prototype.mousedown = function(ev) {
	var mousePoint = mousePos(ev, "canvasObject", this.canvasOffset.x, this.canvasOffset.y);
	var x = mousePoint.x;
	var y = mousePoint.y;
	
	for (var i = 0 ; i < this.points.length; i++){
		var point = this.points[i];
		if (point.onXY(x,y)){
			this.draggedPoint = point;
			this.draggedPoint.setDragged(true);
		}
	}
}

// Tracks mouse movement and updates point locations.
PTZBuild2D.prototype.mousemove = function(ev) {
	if (this.draggedPoint){
		var mousePoint = mousePos(ev, "canvasObject", this.canvasOffset.x, this.canvasOffset.y);
		var x = mousePoint.x;
		var y = mousePoint.y;
	
		this.draggedPoint.setPosition(x,y);
		this.drawEverything();
		this.draggedPoint.movedPoint = true;
	}
}

PTZBuild2D.prototype.drawEverything = function(){
	drawImage();
	for (var i in this.points){
		this.points[i].draw()
	}
}

PTZBuild2D.prototype.validateMovedPoints = function(event){
	for (var i in this.points){
		if (! this.points[i].movedPoint){
			return PTZ_FILL_STYLES[i];
		}
	}
	return '';
}

PTZBuild2D.prototype.dumpToHTML = function(){
	document.getElementById('noPoints').value = this.numPoints;
	var uStr = this.points[0].u;
	var vStr = this.points[0].v;
	for (var i = 1; i < this.numPoints; i++){
		uStr = uStr + ' ' + this.points[i].u ;
		vStr = vStr + ' ' + this.points[i].v ;
	}
	document.getElementById("u").value = uStr;
	document.getElementById("v").value = vStr;
}


function PTZBuild3D(lat, lon, alt){
	this.draggedPoint = null;
	this.movedPoint = false;
	this.initPoints(lat,lon,alt);
	
	var me = this;
	google.earth.addEventListener(ge.getGlobe(), "mousedown", function(event){me.geMouseDownHandler(event)});
	google.earth.addEventListener(ge.getGlobe(), "mouseup", function(event){me.geMouseUpHandler(event)});
}

PTZBuild3D.prototype.initPoints = function(lat,lon,alt){
	for (var i in this.points){
		this.points[i].undraw();
	}
	
	var type = "";
	for (i=0; i< document.corrForm.correspondence.length; i++)
	{
		if (document.corrForm.correspondence[i].checked==true)
		{
			type = document.corrForm.correspondence[i].value;
			break;
		}
	}
	if (type != ""){
		this.numPoints = parseInt(type);
		this.points = new Array();
		if (this.numPoints == 1){
			this.points[0] = new Point3D(lat,lon,alt, PTZ_3D_ICONS[0]);
			this.points[0].movedPoint = false;
			this.points[0].draw();
		} else {
			var theta = 0;
			var thetaStep = 2 * Math.PI / this.numPoints;
			for (var i = 0; i < this.numPoints; i++){
				var x = lon + Math.cos(theta) * 0.00001 / 4;
				var y = lat + Math.sin(theta) * 0.00001 / 4;
				this.points[i] = new Point3D(x,y,alt, PTZ_3D_ICONS[i]);
				this.points[i].movedPoint = false;
				this.points[i].draw();
				theta = theta + thetaStep;
			}
		}
		
	}
}

PTZBuild3D.prototype.geMouseMoveHandler = function(event){
	if (this.draggedPoint != null){
		this.draggedPoint.movePoint(event);
	}
}

// handles all google earth mouse down events.
PTZBuild3D.prototype.geMouseDownHandler = function(event){
	this.geMouseUpHandler(event);
	for (var i = 0; i < this.points.length; i++){
		if (event.getTarget().equals(this.points[i].placemark)){
			this.draggedPoint = this.points[i];
			this.draggedPoint.lastY = -1;
			var me = this;
			google.earth.addEventListener(this.draggedPoint.placemark, "mousemove", function(event){me.geMouseMoveHandler(event)});
			break;
		}
	}
	registerClick()
}

// handles all google earth mouse up events.
PTZBuild3D.prototype.geMouseUpHandler = function(event){

	if (this.draggedPoint != null){
		var me = this;
		google.earth.removeEventListener(this.draggedPoint.placemark, "mousemove", function(event){me.geMouseMoveHandler(event)});
		this.draggedPoint.lastY = -1;
		this.draggedPoint = null;
	}
}

PTZBuild3D.prototype.centerPoints = function(event){
	if (this.points.length == 1){
		var point = this.points[0];
		var ht = absoluteHitTestPercent(0.5, 0.5);
		if (ht != null){
			point.undraw();
			point.setLoc(ht.lat, ht.lon, ht.alt);
			point.movedPoint = false;
			point.draw();
		}
	}
	else{
		var theta = 0;
		var thetaStep = 2 * Math.PI / this.points.length;
		for (var i in this.points){
			var point = this.points[i];
			var ht = absoluteHitTestPercent(0.5 + 0.25 * Math.cos(theta), 0.5 + 0.25 * Math.sin(theta));
			if (ht != null){
				point.undraw();
				point.setLoc(ht.lat, ht.lon, ht.alt);
				point.movedPoint = false;
				point.draw();
			}
			theta = theta + thetaStep;
		}
	}
}

PTZBuild3D.prototype.validateMovedPoints = function(event){
	for (var i in this.points){
		if (! this.points[i].movedPoint){
			return PTZ_FILL_STYLES[i];
		}
	}
	return '';
}

PTZBuild3D.prototype.dumpToHTML = function(){
	var latStr = this.points[0].lat;
	var lonStr = this.points[0].lon;
	var altStr = this.points[0].alt;
	for (var i = 1; i < this.numPoints; i++){
		latStr = latStr + ' ' + this.points[i].lat ;
		lonStr = lonStr + ' ' + this.points[i].lon ;
		altStr = altStr + ' ' + this.points[i].alt;
	}
	
	document.getElementById("lat").value = latStr;
	document.getElementById("lon").value = lonStr;
	document.getElementById("alt").value = altStr;
}